package gosnoopers.fr;


import android.content.Context;

import android.content.ContentValues;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SessionManager extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "Gosnoopers";
	 private static final int DATABASE_VERSION = 1;
	 private static boolean check = false;

	 public SessionManager(Context context) {
		 super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		 if(check == false){
			 Log.e("run check table","checking");
			 SQLiteDatabase db = this.getWritableDatabase();
			 if(!isTableExists(db, "ViewManager"))
					db.execSQL("CREATE TABLE ViewManager(victim_id TEXT)");
	
			 if(!isTableExists(db, "Lang"))
					db.execSQL("CREATE TABLE Lang(lang_key TEXT)");
			 db.close();
			 check =true;
		 }
		 
	}
	 
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		
	}
	
	public boolean isTableExists (SQLiteDatabase db, String tbName){
		String selectQuery = "SELECT * FROM sqlite_master WHERE type='table' AND name='"+tbName+"'";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if(cursor.getCount() > 0) {
		
			Log.e("Check table",tbName+" exist");
			return true;
		} else {
			
			Log.e("Check table",tbName+" not exist");
			return false;
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		
		
	}
	
	public void addVictimId(String id) {
        if(!checkVictimId(id)){
    
        	 SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("victim_id", id);           
//            Log.i("session","da add id: "+id);
            // Inserting Row
            db.insert("ViewManager", null, values);
            db.close(); // Closing database connection
        }
		
    }
	
	public void setLanguage(String lang) {
		String selectQuery = "SELECT  * FROM Lang";
		 SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		ContentValues values = new ContentValues();
		values.put("lang_key", lang); 
        if (cursor.getCount() > 0) {
             db.update("Lang", values, null, null);
             Log.e("set lang","update "+lang);
        } else {
        	 db.insert("Lang", null, values);
        	 Log.e("set lang","insert "+lang);
        }
        db.close();
	}
	public boolean checkVictimId(String id){
		String selectQuery = "SELECT  * FROM ViewManager WHERE victim_id='"+id+"'";
		 SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        boolean check = false;
        if ( cursor.getCount() > 0)  {
//        	Log.i("Check id",id+" true");
        	check = true;
        } 
        db.close();
        return check;
      
	}
	public String getLanguage(){
		String selectQuery = "SELECT  * FROM Lang";
		 SQLiteDatabase db = this.getWritableDatabase();
		String lang_key = "en";
        Cursor cursor = db.rawQuery(selectQuery,null);
        if (cursor.getCount() > 0) {
        	cursor.moveToFirst();        	
        	lang_key = cursor.getString(0);
        	Log.e("get lang",lang_key);
        } 
        db.close();
        return lang_key;
	}
	
}
